package com.liujit.bms.app.service.impl;

import cn.hutool.http.HtmlUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogArticle;
import com.liujit.bms.app.domain.params.BlogMemberReadRecordParams;
import com.liujit.bms.app.domain.query.BlogArticleQuery;
import com.liujit.bms.app.domain.query.BlogTimeArchiveQuery;
import com.liujit.bms.app.domain.result.BlogArticleResult;
import com.liujit.bms.app.domain.result.BlogTimeArchiveResult;
import com.liujit.bms.app.mapper.BlogArticleMapper;
import com.liujit.bms.app.service.BlogArticleLabelService;
import com.liujit.bms.app.service.BlogArticleService;
import com.liujit.bms.app.service.BlogCategoryService;
import com.liujit.bms.app.service.BlogMemberReadRecordService;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 博客文章Service实现类
 * @Author liujun
 * @Date 2021-07-13 11:11:02
 */
@RequiredArgsConstructor
@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements BlogArticleService {

    private final BlogArticleLabelService blogArticleLabelService;

    private final BlogCategoryService blogCategoryService;

    private final BlogMemberReadRecordService blogMemberReadRecordService;

    @Override
    public PageBean<BlogArticleResult> getPage(BlogArticleQuery query) {
        Page<BlogArticleResult> page = Pagination.page(query);
        query.setIsOpen(GlobalConstant.OPEN_YES);
        IPage<BlogArticleResult> iPage = super.baseMapper.selPage(page, query);
        PageBean<BlogArticleResult> pageBean = new PageBean<>();
        pageBean.setTotal(iPage.getTotal());
        pageBean.setPages(iPage.getPages());
        pageBean.setPageNumber(iPage.getCurrent());
        pageBean.setPageSize(iPage.getSize());
        List<BlogArticleResult> records = iPage.getRecords();
        if (records == null) {
            pageBean.setRows(new ArrayList());
        } else {
            records.forEach(item -> {
                item.setContent(HtmlUtil.cleanHtmlTag(item.getContent()).substring(0, 100));
            });
            pageBean.setRows(records);
        }
        pageBean.setSize(pageBean.getRows().size());
        return pageBean;
    }

    @Override
    public BlogArticleResult get(Long id) {
        BlogArticle blogArticle = super.getById(id);
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        BlogArticleResult result = DozerConvertor.convertor(blogArticle, BlogArticleResult.class);
        // 分类名称
        result.setCategoryName(blogCategoryService.getNameById(result.getCategoryId()));
        // 标签
        result.setLabel(blogArticleLabelService.getLabelByArticleId(id));
        // 添加阅读记录
        BlogMemberReadRecordParams blogMemberReadRecord = new BlogMemberReadRecordParams();
        blogMemberReadRecord.setArticleId(result.getId());
        blogMemberReadRecord.setTitle(result.getTitle());
        blogMemberReadRecord.setCoverImage(result.getCoverImage());
        blogMemberReadRecordService.add(blogMemberReadRecord);
        return result;
    }

    @Override
    public List<BlogTimeArchiveResult> getTimeArchive(BlogTimeArchiveQuery query) {
        return super.baseMapper.selTimeArchive(query);
    }
}
